package uia.alumni.profile;

import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.http.HttpServletRequest;
import uia.alumni.data.CvEntry;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.web.Page;



/**
 * Make a page for editing the logged in user's cv.
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class EditCv  extends Page implements Constants {

    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "profile.editcv";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;


    public EditCv() {
        super(Role.member);
    }


    /**
     * Write a form for editing the logged in user's
     * cv.
     * @param out
     * @param request
     */
    public void writeBody(PrintWriter out, HttpServletRequest request) {
        EntityManager em = emf.createEntityManager();
        User user = getLoggedInUser(em, request);

        out.println("<h1>Editing "+user.getFullName()+"'s CV</h1>");
        out.println("<table>");
        out.println("<tr><th>When</th><th>What</th></tr>");
        out.print("<tr><td/><td>New Entry</td><td>");
        printLink(out,"[Add]", EditCvEntry.SERVLET_NAME);
        out.println("</td></tr>");
        for (CvEntry cve : user.getCv()) {
            out.print("<tr><td>"
                    +cve.getFromDate()+"-<br/>"+cve.getToDate()
                    +"</td><td><b>"+cve.getType()+"</b><br/>"
                    +cve.getDescription()+"</td><td>");
            printLink(out, "[Edit]", EditCvEntry.SERVLET_NAME,
                    CVENTRY, String.valueOf(cve.getId()));
            out.print("</td><td>");
            printLink(out, "[Delete]", DeleteCvEntry.SERVLET_NAME,
                    CVENTRY, String.valueOf(cve.getId()));
            out.print("</td></tr>");
        }
        out.println("</table>");
    }

    public final static long serialVersionUID = 1;

}
